package org.jfk.tbox.net;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jfk.core.utils.ByteUtils;
import org.jfk.tbox.model.TboxMsgLog;
import org.jfk.tbox.net.app.DSRC_AbnormalAlarm.DSRC_AbnormalAlarmReq;
import org.jfk.tbox.net.app.TBOX_AbnormalAlarm.TBOX_AbnormalAlarmReq;
import org.jfk.tbox.net.app.TBOX_BCall.TBOX_BCallReq;
import org.jfk.tbox.plugin.TboxTcpServerPlugin;


public class TcpServerHandler extends SimpleChannelHandler{

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {

		super.messageReceived(ctx, e);
		
		ChannelBuffer buf = (ChannelBuffer) e.getMessage();
		byte[] b = buf.array();
		System.out.println(System.currentTimeMillis() + " before = " + ByteUtils.byteToHexString(b));
		byte[] a = ByteUtils.ASCIIBufferToHexBuffer(b);
		System.out.println(System.currentTimeMillis() + " after  = " + ByteUtils.byteToHexString(a));

		TboxMsg msg = TboxMsgParser.loadMsg(a);
		System.out.println(msg);
		
		int alertType = 0;
		
		if (msg instanceof DSRC_AbnormalAlarmReq){
			alertType = 101120;
		}else if (msg instanceof TBOX_AbnormalAlarmReq){
			TBOX_AbnormalAlarmReq msg1 = (TBOX_AbnormalAlarmReq) msg;
			if (msg1.getAlarmType() == 1){
					
				alertType = 101101;
				
			}else if (msg1.getAlarmType() == 2){

				alertType = 101102;
				
			}else if (msg1.getAlarmType() == 3){

				alertType = 101103;

			}
		}else if (msg instanceof TBOX_BCallReq){
			alertType = 101130;
		}
		String ip = e.getChannel().getRemoteAddress().toString();
		System.out.println("ip === " + ip);
		TboxMsgLog msgLog = new TboxMsgLog();
		msgLog.set("aid", msg.appId)
		.set("mid", msg.msgId)
		.set("eventId", msg.eventId)
		.set("sn", msg.sn)
		.set("imsi", msg.imsi)
		.set("vin", msg.vin)
		.set("eventTime", msg.eventCreateTime)
		.set("appData", ByteUtils.byteToHexString(msg.appData))
		.set("data", ByteUtils.byteToHexString(a))
		.set("alertType", alertType)
		.set("ip", ip)
		.save();
				
		TboxTcpServerPlugin.onlineTerminals.put(msg.sn, ctx.getChannel());
		TboxTcpServerPlugin.onlineTerminals.put(ctx.getChannel(), msg.sn);
		
		byte[] resp = msg.getResp();
		if (resp != null && resp.length > 0){
			String sResp = ByteUtils.byteToHexString(resp);
			System.out.println(System.currentTimeMillis() + " resp  = " + sResp);
			
			byte[] _resp = ByteUtils.strToASCII(sResp);
			ChannelBuffer cb = ChannelBuffers.wrappedBuffer(_resp) ;
			e.getChannel().write(cb,e.getRemoteAddress());//兼容UDP，获取对端IP与端口。
		}else{
			System.out.println(System.currentTimeMillis() + " resp  = " + "无回包");
		}

	}

	@Override
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		Object sn = TboxTcpServerPlugin.onlineTerminals.get(ctx.getChannel());
		TboxTcpServerPlugin.onlineTerminals.remove(sn);
		TboxTcpServerPlugin.onlineTerminals.remove(ctx.getChannel());
		super.channelClosed(ctx, e);
	}

	@Override
	public void disconnectRequested(ChannelHandlerContext ctx,
			ChannelStateEvent e) throws Exception {
		Object sn = TboxTcpServerPlugin.onlineTerminals.get(ctx.getChannel());
		TboxTcpServerPlugin.onlineTerminals.remove(sn);
		TboxTcpServerPlugin.onlineTerminals.remove(ctx.getChannel());
		super.disconnectRequested(ctx, e);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {
		Object sn = TboxTcpServerPlugin.onlineTerminals.get(ctx.getChannel());
		TboxTcpServerPlugin.onlineTerminals.remove(sn);
		TboxTcpServerPlugin.onlineTerminals.remove(ctx.getChannel());
		super.exceptionCaught(ctx, e);
	}


}
